<template>
  <!-- 添加或修改项目基础信息管理对话框 -->
  <el-dialog :close-on-click-modal="false" :title="title" :visible.sync="open" append-to-body v-dialogDrag width="90%">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="98px">
      <el-form-item label="工程名称" prop="gongchengName">
        <el-input v-model="queryParams.gongchengName" placeholder="请输入工程名称" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="产品编码" prop="goodsCode">
        <el-input v-model="queryParams.goodsCode" placeholder="请输入产品编码" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="产品名称" prop="goodsName">
        <el-input v-model="queryParams.goodsName" placeholder="请输入产品名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="产品名称" prop="model">
        <el-input v-model="queryParams.model" placeholder="请输入产品名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="产品状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择产品状态" clearable>
          <el-option v-for="dict in dict.type.goods_status" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <div v-if="selectedRows.length > 0">
      <span>已勾选数量合计: {{selectedRows.reduce((acc, row) => acc + row.orderCount, 0)}}</span>
    </div>
    <el-table show-summary :summary-method="totalOutYear" :data="columns" ref="dragTable" row-key="id"
      v-loading="loading" @sort-change="handleSortChange" @selection-change="handleSelectionChange" :max-height="maxHeight">
      <el-table-column type="selection" width="100" />
      <el-table-column align="center" label="序号" type="index" width="50">
        <template slot-scope="scope">
          <span>{{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}</span>
        </template>
      </el-table-column>
      <el-table-column sortable label="工程名称" align="center" prop="gongchengName" />
      <el-table-column sortable label="产品编码" align="center" prop="goodsCode" />
      <el-table-column sortable align="center" label="产品状态" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.goods_status" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column align="center" label="客户名称" prop="order.companyName" />
      <el-table-column align="center" label="业务员" prop="order.salesmanName" />
      <el-table-column sortable align="center" label="产品名称" prop="goodsName"  show-overflow-tooltip/>
      <el-table-column sortable align="center" label="产品型号" prop="model" />
      <el-table-column align="center" label="产品单位" prop="unit" />
      <!-- <el-table-column align="center" label="项目名称" prop="model"/> -->
      <el-table-column sortable label="需求日期" align="center" prop="deliveryTime" width="120">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.deliveryTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column align="center" label="销售单价" prop="goodsUnitPrice"/> -->
      <el-table-column align="center" sortable label="订单数量" prop="orderCount" />
      <el-table-column align="center" sortable label="锁定数量" prop="lockCount" />
      <!--<el-table-column align="center" label="方式" prop="isDo">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.isdo" :value="scope.row.isDo"/>
          </template>
        </el-table-column>-->
        <el-table-column align="center" sortable label="自制/请购数量" prop="pleaseBuyCount"/>
        <el-table-column align="center" sortable label="备注" prop="remark"/>
        <el-table-column align="center" class-name="small-padding fixed-width" label="操作">
          <template slot-scope="scope">
            <el-button size="mini" type="text" icon="el-icon-search" @click="choseGoodsDeatil(scope.row)">锁定产品明细</el-button>
          </template>
        </el-table-column>
    </el-table>
    <choseGoodsDeatil-form-list ref="choseGoodsDeatilFormList" @ok="getList"></choseGoodsDeatil-form-list>
    <pagination :limit.sync="queryParams.pageSize" :page.sync="queryParams.pageNum" :total="total" @pagination="getList"
      v-show="total > 0" />
    <div class="dialog-footer" slot="footer">
      <el-button @click="cancel">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>

import { addAction, deleteAction, getAction, getById } from '@/api/manage'
import ChoseGoodsDeatilFormList from '@/views/develop/goodsSaleMaterial/module/ChoseGoodsDeatilFormList'

export default {
  name: "OrderGoodsList",
  dicts: ['isdo', 'goods_status'],
  components: {
    ChoseGoodsDeatilFormList
  },
  data() {
    return {
      maxHeight: (window.innerHeight * 0.9 - 260) ,
      // 遮罩层
      loading: true,
      activeName: "first",
      // 弹出层标题
      title: "订单产品明细",
      // 是否显示弹出层
      open: false,
      // 显示搜索条件
      showSearch: true,
      // 表单参数
      form: {},
      proId: "",
      proStatus: "",
      columns: [],
      total: 0,
      // 表单校验
      rules: {
      },
      queryParams: {
        pageNum: 1,
        pageSize: 20,
        id: null,
        goodsCode: null,
        code: null,
        goodsName: null,
        gongchengName: null,
        model: null,
        isdo: null,
        status: null,
        remark: null,
        projectId: null,
        orderByColumn: "isdo",
        isAsc: "desc"
      },
      url: {
        list: '/goodsSale/goodsSale/list',
        getById: '/projectBaseInfo/projectBaseInfo/',
      },
      selectedRows: [], // 用于存储勾选的行数据
    };
  },
  created() {
    this.handleQuery();
  },
  methods: {
    handleSelectionChange(selection) {
      this.selectedRows = selection;
    },
    totalOutYear(param) {
      const { columns, data } = param
      const sums = []
      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = '合计';
          return;
        }
        if (index === 1 || index === 2 || index === 3 || index === 4 || index === 5 || index === 6 || index === 8 || index === 9 || index === 7 || index === 10 || index === 14) {
          sums[index] = '';
          return;
        }
        const values = data.map(item => Number(item[column.property]));
        if (column.property != 'implementYear') {
          sums[index] = values.reduce((prev, curr) => {
            const value = Number(curr);
            if (!isNaN(value)) {
              return prev + curr;
            } else {
              return prev;
            }
          }, 0);
          sums[index];
        }
      });
      return sums
    },
    /** 排序触发事件 */
    handleSortChange(column, prop, order) {
      this.queryParams.orderByColumn = column.prop;
      this.queryParams.isAsc = column.order;
      this.handleQuery();
    },
    choseGoodsDeatil(row) {
      this.$refs.choseGoodsDeatilFormList.show(row,"1");
    },
    getList() {
      this.loading = true;
      this.queryParams.projectId = this.proId;
      getAction(this.url.list, this.queryParams).then(response => {
        this.columns = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    reset() {
      this.form = {
        id: null,
        remark: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      this.proId = id;
      this.proStatus = row.status;
      getById(this.url.getById, id).then(response => {
        this.open = true;
        this.title = "修改订单基础信息管理";
        this.getList();
      });
    },

    handleOrderGoodsShow(row) {
      this.open = true;
      this.proId = row.id;
      this.getList();
    },

  }
};
</script>
